package com.server;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Helper.CustomerHelper;
import com.Helper.PaymentHelper;
import com.Helper.ReservationHelper;
import com.Helper.ReservationInfo;
import com.Helper.SeatHelper;
import com.database.*;

public class MakePaymentServlet extends CustomerServlet
{
	private static final long serialVersionUID = 1L;
	String username = null;
	String msg = null;
	String firstName = null;
	String lastName = null;
	
		
	public boolean IsStringNull(String s)
	{
		if (s == null || s.equals(""))
			return true;
		else
			return false;
	}
		
	public boolean HasNullInputs(String s0, String s1, String s2, String s3)
	{
		if (IsStringNull(s0)) return true;
		else if (IsStringNull(s1)) return true;
		else if (IsStringNull(s2)) return true;
		else if (IsStringNull(s3)) return true;
		else return false;
	}
		
	private void ConnectingFlightPayment(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, SQLException
	{
		String meal1 = req.getParameter("mealOption1");
		String seat1 = req.getParameter("seatID1");
		String flightID1 = req.getParameter("flightID1");
		
		String meal2 = req.getParameter("mealOption2");
		String seat2 = req.getParameter("seatID2");
		String flightID2 = req.getParameter("flightID2");
		
		if (HasNullInputs(firstName, lastName, meal1, seat1) || HasNullInputs(flightID1, meal2, seat2, flightID2))
		{
			msg = "You have not entered all required fields.";
			req.setAttribute("username", username);
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
		}
		else if (SeatHelper.IsSeatReserved(seat1, flightID1))
		{
			msg = seat1 + " on flight " + flightID1 + " is taken by another customer.";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
		}
		else if (SeatHelper.IsSeatReserved(seat2, flightID2))
		{
			msg = seat1 + " on flight " + flightID2 + " is taken by another customer.";
			req.setAttribute("msg", msg);
			req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
		}
		else
		{
			float price1 = PaymentHelper.GetPriceForSeat(seat1, flightID1);
			float price2 = PaymentHelper.GetPriceForSeat(seat2, flightID2);
			
			req.setAttribute("firstName", firstName);
			req.setAttribute("lastName", lastName);
			req.setAttribute("username", username);
			req.setAttribute("meal1", meal1);
			req.setAttribute("meal2", meal2);
			req.setAttribute("flightID1", flightID1);
			req.setAttribute("flightID2", flightID2);
			req.setAttribute("seat1", seat1);
			req.setAttribute("seat2", seat2);
			req.setAttribute("price1", price1);
			req.setAttribute("price2", price2);
			req.getRequestDispatcher("reservation/make_connecting_payment.jsp").forward(req, resp);
		}
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
	{
		String connecting = null;
		firstName = req.getParameter("firstName");
		lastName = req.getParameter("lastName");
		username = req.getParameter("username");
		
		ConnectDB();
		
		connecting = req.getParameter("connecting");
		
		try //if the seat is not taken, user can make a payment
		{
			SeatHelper.Setup(connection);
			PaymentHelper.Setup(connection);
			ReservationHelper.Setup(connection, username);
			
			if (connecting.equals("1"))
			{
				ConnectingFlightPayment(req, resp);
			}
			else
			{
				String meal = null;
				String seat = null;	
				String flightID = null;
							
				meal = req.getParameter("mealOption");
				flightID = req.getParameter("flightID");
				seat = req.getParameter("seatID");
				
				if (HasNullInputs(firstName, lastName, meal, seat) == true)
				{
					msg = "You have not entered all required fields.";
					req.setAttribute("username", username);
					req.setAttribute("flightID", flightID);
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}		
				else if (SeatHelper.IsSeatReserved(seat, flightID))
				{
					msg = "This seat is taken by another customer";
					req.setAttribute("username", username);
					req.setAttribute("flightID", flightID);
					req.setAttribute("msg", msg);
					req.getRequestDispatcher("reservation/payment_error.jsp").forward(req, resp);
				}
				else
				{
					float price = PaymentHelper.GetPriceForSeat(seat, flightID);
					
					req.setAttribute("firstName", firstName);
					req.setAttribute("lastName", lastName);
					req.setAttribute("meal", meal);
					req.setAttribute("username", username);
					req.setAttribute("flightID", flightID);
					req.setAttribute("seat", seat);
					req.setAttribute("price", price);
					req.getRequestDispatcher("reservation/make_payment.jsp").forward(req, resp);
				}
			}
		} 
		catch (SQLException e1) 
		{
			msg = "Reservation Failed.";
			e1.printStackTrace();
			req.setAttribute("msg", msg);
			req.setAttribute("username", username);
			req.getRequestDispatcher("index_loggedin.jsp").forward(req, resp);	
		}
				
		
	}

}

